// page/address/address.js\
const {
  $Toast
} = require('../../dist/base/index');
const {
  $Message
} = require('../../dist/base/index');
var app = getApp();
const server = require("../../utils/server.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    curId: '',
    inputWarning: false,
    addrType: 1, //0为修改  1为新增
    username: "",
    phone: "",
    address: "",
    formInput: false,
    addrList: [],
    addrDefault: "点击选择",
    lng: 0,
    lat: 0,
    visible5:false,
    actions5: [{
        name: '取消'
      },
      {
        name: '删除',
        color: '#ed3f14',
        loading: false
      }
    ],
    visible1:false,
    actions1: [
            {
                name: '设为默认地址',
            },
            {
                name: '删除',
              
            }
             
        ],
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
      this.setData({
      orderData:options.orderData
      })
    this.getAddrList();
  },
  onReady: function() {

  },
  onShow: function() {

  },
  onHide: function() {

  },
  onUnload: function() {
  
     
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 获取输入账号 
  phoneInput: function(e) {
    this.setData({
      phone: e.detail.detail.value
    })
  },

  // 获取输入密码 
  usernameInput: function(e) {

    this.setData({
      username: e.detail.detail.value
    })
  },
  addInput: function(e) {
    this.setData({
      address: e.detail.detail.value
    })
  },
  handleCancel1(){
    this.setData({
      visible1: false,
    })
  },
  handleClickItem1({detail}){
    let that=this;
   if(detail.index==0){
     that.setData({
       visible1: false,
     })
     that.setDefault();
   }else if(detail.index==1){
   that.setData({
     visible1:false,
     visible5:true
   })
   }
  },
  chooserAddr() {
    let that = this;
    wx.chooseLocation({
      success: function(res) {
      
        that.setData({
          addrDefault: res.address,
          lng: res.longitude,
          lat: res.latitude,
        })
      }
    });
  },
  changeData(e) {
    let that = this;

    let info = e.currentTarget.dataset.info;

    that.setData({
      username: info.name,
      phone: info.phone,
      lng: info.longitude,
      lat: info.latitude,
      address: info.site,
      addrType: 0,
      formInput: true,
      curId: info.id
    })
  },
  addData() {
    this.setData({
      addrType: 1,
      formInput: true,
    })
  },
  cancelForm() {
     
    this.setData({
      username: "",
      phone: '',
      lng: 0,
      lat: 0,
      addrDefault:"点击选择",
      address: '',
      addrType: 1,
      formInput: false,
      curId: ''
    })
  },
  delAddr(e) {
    this.setData({
      visible1: true,
      curId: e.currentTarget.dataset.info.id,
      curInfo: e.currentTarget.dataset.info
    })
    //  this.setData({
    //    visible5:true,
    //    curId: e.currentTarget.dataset.id
    //  })
  },
  //设置默认地址
  setDefault(){
    let that=this;
    var info=that.data.curInfo;
    info.isDefault=1;
    server.postReq("/mobile/PersonalCenter/updateReceiverAddress",info,function (res) {
      if (res.data.code == 1) {
        that.handleSuccess("设置成功");
        that.getAddrList();
        that.setData({
          username: "",
          phone: '',
          lng: 0,
          lat: 0,
          addrDefault: "点击选择",
          address: '',
          addrType: 1,
          formInput: false,
          curId: ''
        })
      }
    })
  },
  delForm({detail}) {
    let that = this;
    if(detail.index==0){
      that.setData({
        visible5: false,
      });
    }else{
     server.postReq('/mobile/PersonalCenter/deleteReceiverAddress', {
        id: that.data.curId
      },function (res) {
        if (res.data.code == 1) {
          const action = [...that.data.actions5];
          action[1].loading = true;

          that.setData({
            actions5: action
          });
          
          setTimeout(() => {
            action[1].loading = false;
            that.setData({
              actions5: action,
              visible5: false,
             });
          
            that.getAddrList();
         
            that.handleSuccess(res.data.msg);
          }, 1500);
        }
      })
    }
  
  },
  sumitAddress() {
    let that = this;

    var obj = new Object();
    if (this.data.phone == '' || this.data.address == '' || this.data.username == '') {
      this.handleWarning('请完善信息');
    } else {
      obj.userId = wx.getStorageSync('uid');
      obj.name = this.data.username;
      obj.site = this.data.address;
      obj.phone = this.data.phone;
      obj.isDefault = 1;
      obj.longitude = this.data.lng;
      obj.latitude = this.data.lat;
   
      if (that.data.addrType == '0') {
        //修改地址
        var url = '/mobile/PersonalCenter/updateReceiverAddress';
        obj.id = this.data.curId;

      } else if (that.data.addrType == '1') {
        //新增地址
         var url = '/mobile/PersonalCenter/addReceiverAddress';
      }
      server.postReq(url, obj, function(res) {
        if (res.data.code == 1) {
          that.handleSuccess(res.data.msg);
          that.getAddrList();
          that.setData({
            username: "",
            phone: '',
            lng: 0,
            lat: 0,
            addrDefault: "点击选择",
            address: '',
            addrType: 1,
            formInput: false,
            curId: ''
          })
        }else{
          that.handleText(res.data.msg);
        }
      })
      //  setTimeout(()=>{
      //    wx.switchTab({
      //      url: '../mine/mine',
      //    })
      //  },1000)
    }
  },
  getAddrList(){
    let that = this;
    let uid = wx.getStorageSync('uid');
    server.postReq("/mobile/PersonalCenter/lookReceiverAddress", {
      userId: uid
    }, function(res){
      if (res.data.code == 1) {
        that.setData({
          addrList: res.data.data
        })
      }
    })
  },
  handleSuccess(successMsg) {
    $Toast({
      content: successMsg,
      type: 'success'
    });
  },
  handleWarning(mes) {
    $Toast({
      content: mes,
      type: 'warning'
    });
  },
  handleError() {
    $Toast({
      content: '错误的提示',
      type: 'error'
    });
  },
  handleLoading() {
    $Toast({
      content: '加载中',
      type: 'loading'
    });
  },

})